/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#pragma once

#include <map>
#include <list>
#include <set>
#include <algorithm>

#include "MailingListSubscriptionCatagoryEnumerations.h"
#include "Message.h"
#include "..\XSystemComponents\SystemComponent.h"

namespace XF
{
	class Messenger;

	class PostOffice : public SystemComponent
	{
	private:
		typedef std::map<XF::MailingList::ENUM, std::list<int>> MailingListMap;
		typedef std::map<XF::MailingList::ENUM, std::list<int>>::const_iterator MailingListMapIterator;
		typedef std::map<std::string, int> PhonebookMap;

		MailingListMap mMailingListSubscriptions;
		std::map<int, Messenger*> mAllMessengers;
		std::set<Message> mPendingMessages;
		PhonebookMap mPhonebook;

		float mTime;

		void Dispatch(const Message &message);

	public:
		PostOffice(Application *ownerApp);
		~PostOffice();

		void RegisterInPhonebook(std::string name, int id);
		void UnregisterFromPhonebook(std::string name);
		int LookupPhonebook(std::string name);
		bool LookupPhonebookSafe( std::string name, int &id ); //chris for looking up in run window

		bool SubscribeToMailingList(XF::MailingList::ENUM mlist, Messenger *messenger); //i modified this to a bool
		void UnsubscribeFromMailingList(XF::MailingList::ENUM mList, Messenger *messenger);

		void PostAMessage(int receiverID, int senderID, MessageSubject::ENUM subject, void* data, float sendDelay);
		void PostAMessage(XF::MailingList::ENUM mailingList, int senderID, MessageSubject::ENUM subject, void* data, float sendDelay);

		void RegisterMessenger(Messenger *messenger);
		void UnregisterMessenger(Messenger *messenger);

		bool OnInitialise();
		void OnShutDown();
		void OnUpdate(double dT);
	
		bool LookUpPhoneBookName(int id, std::string& name);
	};
}
